package com.imti.mall.sale.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.feign.clients.PmsProductCategoryFeignClient;
import com.imti.mall.model.pms.pojo.PmsBrand;
import com.imti.mall.model.sms.pojo.SmsFlashPromotion;
import com.imti.mall.model.sms.pojo.SmsHomeBrand;
import com.imti.mall.model.sms.vo.SmsHomeBrandVo;
import com.imti.mall.sale.dao.SmsHomeBrandDao;
import com.imti.mall.sale.service.SmsHomeBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/sale")
@Api(tags = "首页内容推荐")
public class SmsHomeBrandController {

    @Autowired
    SmsHomeBrandService smsHomeBrandService;


    @Resource
    PmsProductCategoryFeignClient pmsProductCategoryFeignClient;

    /**
     * 查询首页查询品牌推荐列表
     */
    @ApiOperation(value = "查询首页查询品牌推荐列表")
    @PostMapping(value = "/findBrand" )
    public CommonResult findBrand
    ( SmsHomeBrand smsHomeBrand,
      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<SmsHomeBrand> list = smsHomeBrandService.findBrand(smsHomeBrand,pageSize, pageNum);
        if(list.size()>0){
            return CommonResult.success(CommonPage.restPage(list));
        }else {
            return CommonResult.success("未查询到信息");
        }
    }

    /**
     * 添加查询首页查询品牌推荐
     */
    @ApiOperation(value = "添加查询首页查询品牌推荐列表")
    @PostMapping(value = "/addHomeBrand" )
    public CommonResult addHomeBrand(@RequestBody List<SmsHomeBrand> smsHomeBrand) {
        boolean res = smsHomeBrandService.addSmsHomeBrand(smsHomeBrand);
        if(res){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 修改首页查询品牌推荐列表
     */
    @ApiOperation(value = "修改首页查询品牌推荐列表")
    @PostMapping(value = "/updateHomeBrand" )
    public CommonResult updateHomeBrand(SmsHomeBrandVo smsHomeBrandVo) {
            int res = 0;
            if (smsHomeBrandVo.getId() != null) {
                SmsHomeBrand smsHomeBrand = smsHomeBrandVo;
                res = smsHomeBrandService.updateSmsHomeBrand(smsHomeBrand);
            } else {
                Long[] ids = smsHomeBrandVo.getIds();
                for (int i = 0; i < ids.length; i++) {
                    SmsHomeBrand smsHomeBrand = smsHomeBrandVo;
                    smsHomeBrand.setId(ids[i]);
                    res = smsHomeBrandService.updateSmsHomeBrand(smsHomeBrand);
                }
            }
            if (res > 0) {
                return CommonResult.success("修改成功");
            }
            return CommonResult.failed("修改失败");
        }
    

    /**
     * 删除首页查询品牌推荐列表
     */
    @ApiOperation(value = "删除首页查询品牌推荐列表")
    @PostMapping(value = "/deleteHomeBrand" )
    public CommonResult deleteHomeBrand( SmsHomeBrandVo smsHomeBrandVo) {
        Long[] ids= smsHomeBrandVo.getIds();
        int res =0;
        for(int i=0;i<ids.length;i++){
            res = smsHomeBrandService.delSmsHomeBrand(ids[i].intValue());
        }
        if(res>0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }


    /**
     * 查询首页查询品牌推荐列表
     */
   @ApiOperation(value = "查询首页查询品牌推荐列表（app）")
    @PostMapping(value = "/findSmsHomeBrandApp" )
    public CommonResult findSmsHomeBrandApp
    ( SmsHomeBrand smsHomeBrand,
      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<SmsHomeBrand> list = smsHomeBrandService.findBrand(smsHomeBrand, pageSize, pageNum);
        for(int i=0;i<list.size();i++){
            PmsBrand pmsBrand= pmsProductCategoryFeignClient.getPmsBrandByIdTo(list.get(i).getBrandId().intValue());
            list.get(i).setPmsBrand(pmsBrand);
        }
        return CommonResult.success(list);
    }
}
